package cn.ming.wfchat.app.dynamic.activity;

import android.content.Intent;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import butterknife.BindView;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.dialog.PaymentDialog;
import cn.ming.wfchat.app.dynamic.bean.RateBean;
import cn.ming.wfchat.kit.WfcBaseActivity;
import cn.wildfirechat.remote.ChatManager;

/**
 * 选择人数
 */
public class SelectNumberActivity extends WfcBaseActivity {


    @BindView(R.id.tv_text)
    TextView tvText;
    private TextView tv_formula;
    private SeekBar seekbar;
    private TextView tv_number;
    private int number = 0;
    private double rate;
    private double total;
    private int sumCount = 0;


    @Override
    protected int contentLayout() {
        return R.layout.activity_select_number;
    }

    @Override
    protected void afterViews() {
        setTitle("选择人数");
        setTvRight("确认");
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }
        });
        tv_formula = findViewById(R.id.tv_formula);
        seekbar = findViewById(R.id.seekbar);
        tv_number = findViewById(R.id.tv_number);
        init();
    }

    private void init() {

        findDynamicsRate();
        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                number = i;
                total = rate * number;
                initUi();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }


    void findDynamicsRate() {
        AppService.Instance().findDynamicsRate(new AppService.FindDynamicsRateCallback() {
            @Override
            public void onUiSuccess(RateBean assetsBean) {
                if (assetsBean != null) {
                    rate = assetsBean.getRate();
                    total = rate * number;
                    sumCount = assetsBean.getSumCount();
                    seekbar.setMax(sumCount);
                    tvText.setText("发布动态首个好友、普通好友、上下一级好友默认可见，给此级别外好友看见需要按人数收费（" + rate + "FJ）。");
                    initUi();
                }
            }

            @Override
            public void onUiFailure(int code, String msg) {
                ChatManager.showToast(msg);
            }
        });
    }


    void initUi() {
        tv_formula.setText(rate + "*" + number + "=" + total + "FJ");
        tv_number.setText(number + "/" + sumCount);

    }

    void showDialog() {
        if (total > 0) {
            PaymentDialog.newInstance("付款", total + "FJ").setOnClickListener(new PaymentDialog.OnClickListener() {
                @Override
                public void getPwd(String pwd) {
                    Intent intent = new Intent();
                    intent.putExtra("password", pwd);
                    intent.putExtra("conut", seekbar.getProgress()+"");
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }).show(SelectNumberActivity.this);
        } else {
            ChatManager.showToast("请选择人数");
        }

    }

}


